﻿using System;
using System.Diagnostics;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal struct XFPropGradient
    {
        private double _numDegree;
        private double _numFillToBottom;
        private double _numFillToLeft;
        private double _numFillToRight;
        private double _numFillToTop;
        private GradientType _type;

        public XFPropGradient(GradientFill gradientFill)
        {
            _numDegree = gradientFill.Degree;

            switch (gradientFill.GradientType)
            {
                case Spreadsheet.GradientType.Linear:
                    _type = GradientType.Linear;
                    break;
                case Spreadsheet.GradientType.Path:
                    _type = GradientType.Rectangular;
                    break;
                default:
                    throw new NotSupportedException();
            }
            _numFillToLeft = gradientFill.Left;
            _numFillToRight = gradientFill.Right;
            _numFillToTop = gradientFill.Top;
            _numFillToBottom = gradientFill.Bottom;
        }

        public static int Length
        {
            get { return 4 + 8 + 4*8; }
        }

        public void Read(BinaryReader reader)
        {
#if DEBUG
            ReadWriteChecker.CheckStruct((ref XFPropGradient t, BinaryReader r) => t.Read(r),
                                         (ref XFPropGradient t, BinaryWriter w) => t.Write(w), reader, ref this);
#endif

            _type = (GradientType) reader.ReadUInt32();
            _numDegree = reader.ReadDouble();
            _numFillToLeft = reader.ReadDouble();
            _numFillToRight = reader.ReadDouble();
            _numFillToTop = reader.ReadDouble();
            _numFillToBottom = reader.ReadDouble();
        }

        public void Write(BinaryWriter writer)
        {
            Debug.WriteLine("Writing gradient properties, _numDegree = " + _numDegree);
            writer.Write((uint) _type);
            writer.Write(_numDegree);
            writer.Write(_numFillToLeft);
            writer.Write(_numFillToRight);
            writer.Write(_numFillToTop);
            writer.Write(_numFillToBottom);
        }

        public GradientFill ToFill()
        {
            var result = new GradientFill();
            result.Degree = _numDegree;
            switch (_type)
            {
                case GradientType.Linear:
                    result.GradientType = Spreadsheet.GradientType.Linear;
                    break;
                case GradientType.Rectangular:
                    result.GradientType = Spreadsheet.GradientType.Path;
                    break;
                default:
                    throw new NotSupportedException();
            }
            result.Left = _numFillToLeft;
            result.Bottom = _numFillToBottom;
            result.Top = _numFillToTop;
            result.Right = _numFillToRight;

            return result;
        }

        #region Nested type: GradientType

        private enum GradientType
        {
            Linear,
            Rectangular
        }

        #endregion
    }
}